package com.auctionhaus

class Bid {

    Double amount
    Date date
    static Double incrementRate = 0.50

    Customer buyer

    static belongsTo = [buyer:Customer, listing:Listing]            //B5

    static constraints = {
        amount(nullable: false, validator: {val, obj ->
                if (obj.listing != null) {
                    //if the listing is expired, doesn't matter...they can save the bids..
                    if (obj.listing.endDateTime < new Date()) return true
                    //normal validation - checking the bid amount
                    def thisListing = Listing.findByListName(obj.listing?.listName)
                    def maxPrice = thisListing?.getMaxBidPrice()
                    if (!maxPrice)
                        return true
                    else
                        return val >= thisListing.nextBidPrice
                }
                return false

        })
        buyer(nullable:false)
        date(nullable: false)
    }

    static namedQueries = {
       forListing { listId ->
            eq('listing.id', listId)
       }

    }
}
